'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 购买挑战次数
 * @author agl
 */

var _ = require('lodash');
var async = require('async');
var moment = require('moment');
var Code = require('../../../../consts/code');
var moduleType = require('../../../../consts/moduleType');
var operationType = require('../../../../consts/operationType');
var configUtil = require('../../../../util/configUtil');
var constantUtil = require('../../../../util/constantUtil');
var PlayerManager = require('../../../../libs/playerManager');
var taskService = require('../../../../services/taskService');
var roleService = require('../../../../services/roleService');
var dataApi = require('../../../../libs/dataApi.js');
var vipConf = require('../../../../config/vipConf.js');
var expeditionConf = require('../../../../config/expeditionConf.js');

module.exports = function (msg, session, next) {
    var role_id = session.uid;
    var self_hp_info = msg.self_hp_info;
    var rival_hp_info = msg.rival_hp_info;

    var player_manager = new PlayerManager(role_id);
    var hunt_model;
    async.waterfall(
        [
            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(['expedition', 'role', 'accrued', 'task', 'daily_task', 'achieve'], cb);
            },
            function (cb) {
                var expedition_model = player_manager.expedition;
                var role_model = player_manager.role;
                var monster_open_lvl = constantUtil.value('monster_open_lvl');
                if (role_model.lv < monster_open_lvl) {
                    return next(null, {
                        code: Code.USER_LEVEL_NOT_ENOUGH
                    });
                }

                var rival_sum_hp = 0;
                var next_pass_id = expeditionConf.getNextMonsterId(expedition_model.pass_id);
                if (next_pass_id === 99999) {
                    return next(null, {
                        code: Code.EXPEDITION_ALREADY_PASS_ALL
                    });
                }
                var rival_idx = expeditionConf.getMonsterIndexById(next_pass_id);

                // 设置已方血量
                expedition_model.setSoldierCurrHp(self_hp_info);

                // 设置对手血量
                expedition_model.setRivalHp(rival_idx, rival_hp_info);
                _.forEach(rival_hp_info, function (hp_info) {
                    rival_sum_hp += hp_info.curr_hp;
                });

                if (rival_sum_hp === 0) {
                    // 获胜：设置通关副本，设置待开宝箱
                    expedition_model.pass_id = next_pass_id;
                    expedition_model.box_id = expedition_model.pass_id;
                    expedition_model.battle_tm = 0;
                }

                // 任务统计
                taskService.addCompleteMorguiNum(player_manager);

                player_manager.save(cb);
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }

            return next(null, {
                code: Code.OK,
            });
        }
    );
};
